package com.aistudy.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
public class ResourceUtils {
    public static String readResource(String path) {
        try (InputStream is = ResourceUtils.class.getClassLoader().getResourceAsStream(path)) {
            if (is == null) {
                throw new IOException("资源文件未找到: " + path);
            }
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            byte[] data = new byte[1024];
            int bytesRead;
            while ((bytesRead = is.read(data, 0, data.length)) != -1) {
                buffer.write(data, 0, bytesRead);
            }
            buffer.flush();
            return new String(buffer.toByteArray(), StandardCharsets.UTF_8);
        } catch (IOException e) {
            Logger log = LoggerFactory.getLogger(ResourceUtils.class);
            log.error("读取资源文件失败:{}", e.getMessage());
            throw new RuntimeException(e);
        }
    }
}